/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hiof.moraa.server.service;

import com.hiof.moraa.server.dao.SupportLiveLocationDAO;
import com.hiof.moraa.server.service.model.GeoPoint;
import com.hiof.moraa.server.service.model.LiveLocationFeed;
import com.hiof.moraa.server.service.model.Feed;
import com.hiof.moraa.server.service.model.TypeOfAttendant;
import java.sql.Timestamp;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Bergy
 */
@Transactional(propagation = Propagation.REQUIRED,
isolation = Isolation.DEFAULT,
readOnly = false)
public class SupportLiveLocationServiceImp implements LiveLocationService {

    SupportLiveLocationDAO dao;
    MessageDeliveryService messageDeliveryService;

    public void setDao(SupportLiveLocationDAO dao) {
        this.dao = dao;
    }

    public void setMessageDeliveryService(MessageDeliveryService messageDeliveryService) {
        this.messageDeliveryService = messageDeliveryService;
    }

    @Override
    public Feed updateLiveLocationFeed(int userId, int supportId, double lat, double lng, double height, double speed) {
        LiveLocationFeed liveLocationFeed = dao.insertSupportLiveLocationFeed(supportId, lat, lng, height, speed, new Timestamp(new java.util.Date().getTime()));
        messageDeliveryService.deliveryLiveLocationMessage(userId, TypeOfAttendant.SUPPORT, supportId, liveLocationFeed);
        return liveLocationFeed;
    }

    @Override
    public GeoPoint getLatestLocation(int supportId) {
        LiveLocationFeed liveLocationFeed = dao.getLatestLiveLocationFeedOfOneSupport(supportId);
        if (liveLocationFeed != null) {
            return liveLocationFeed.getGoogleGeoPoint();
        } else {
            return null;
        }
    }
}
